<%@page import="org.opengeoingest.tufts.Layer.*, org.OpenGeoPortal.Geoserver.REST.*, java.io.*, org.apache.commons.fileupload.disk.*, java.net.*,
 org.apache.commons.fileupload.*, org.apache.commons.fileupload.servlet.*, java.util.*"%>
 
<jsp:include page="includes/header.jspf" />

<%!
// run the passed command and return the results from it
private static String runCommand(String command)
{
    ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", command);
    processBuilder.redirectErrorStream(true);
    Process process = null;
    try
    {
    	process = processBuilder.start();
    }
    catch (Exception ex)
    {
	System.out.println("cound not create process for command  " + command + ".  error = " + ex.toString());
	return "Error: could not create process for command  " + command + ".  error = " + ex.toString();
    }
    try
    {
	process.waitFor();
    }
    catch (InterruptedException e)
    {
    }
    StringBuilder processLog = new StringBuilder();
    BufferedReader processOutputReader = null;
    try
    {
    	int character;
    	processOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    	while ((character = processOutputReader.read()) != -1)
        {
    	    processLog.append((char) character);
    	}
   }
   catch (Exception ex)
   {
 	return "Error: could not read response for command " + command + " partial response = " + processLog.toString();
   }
	
   return processLog.toString();
}

// is the passed layer found in Solr
// generate an http select query for the passed name
private static boolean isLayerInSolr(String layerName, HttpServletRequest request)
{
    String server = request.getServerName() + ":" + request.getServerPort();    
    String httpRequest = "http://" + server + "/solr/select/?q=Name:" + layerName;
    try
    {
        URL url = new URL(httpRequest);
    	URLConnection connection = url.openConnection();
	InputStream inputStream = connection.getInputStream();
    	BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
    	StringBuilder results = new StringBuilder();
    	String currentLine;
    	while ((currentLine = inputReader.readLine()) != null)
	    results.append(currentLine);
    	String results$ = results.toString();
    	if (results$.contains("numFound=\"1\""))
	    return true;
    	else
	    return false;
    }
    catch (MalformedURLException e)
    {
	System.out.println("MalformedURLException in ingestHandler which checking to see of record is in Solr");
	return false;
    }
    catch (IOException e)
    {
	System.out.println("IOException in ingestHandler which checking to see of record is in Solr");
	return false;
    }

}
%>

<%

/**
This code ingests layers into solr, geoserver and the layer info database table.
Layers are expected to be in FGDC format.
For solr and geserver, it executes curl commands.  This is clearly sub-optimal (but simple).
*/

	DiskFileItemFactory factory = new DiskFileItemFactory();
	ServletFileUpload upload = new ServletFileUpload(factory);

	String institution = request.getParameter("institution");
	// Parse the request and get file items
	if (ServletFileUpload.isMultipartContent(request))
	{
		List items = upload.parseRequest(request);
		// pull institution value from the pulldown menu, only non-file item 
		for (int i = 0 ; i < items.size() ; i++)
		{
			FileItem temp = (FileItem)items.get(i);
			if (temp.isFormField() == true)
				if (temp.getFieldName().equals("institution"))
				institution = temp.getString();
		}
		
		// loop over items looking for the file itmes
		for (int i = 0 ; i < items.size() ; i++)
		{
			FileItem fileItem = (FileItem)items.get(i);
			if (fileItem.isFormField() == false)
			{
				// here with a file, convert it
				DiskFileItem fgdcFile = (DiskFileItem)items.get(i);
				OutputStream solrStream = new ByteArrayOutputStream();
				ToSolr converter = new ToSolr(fgdcFile, solrStream, institution);
				
				InputStream inputStream = fgdcFile.getInputStream();
				Hashtable<BaseGeoParser.Key, Object> values = converter.convert();
				String publicMessage = "(Public Layer)";
				if (((BaseGeoParser.Access)values.get(BaseGeoParser.Key.Access)) == BaseGeoParser.Access.Restricted)
					publicMessage = "(Restricted Layer)";
				out.write("<p><h3>" + fgdcFile.getName() + "</h3>  " + publicMessage + "</p>");
				String esriName = FgdcToSolr.esriLayerNamePrefix + ((String)values.get(BaseGeoParser.Key.EsriName)).toUpperCase();
				//String esriName = ((String)values.get(BaseGeoParser.Key.EsriName)).toUpperCase(); //
				String tempName = fgdcFile.getName();
				if (tempName.endsWith(".shp.xml"))
			     	    tempName = tempName.substring(0, tempName.length() - ".shp.xml".length());
				tempName = "GISPORTAL.GISOWNER01." + tempName;
				tempName = tempName.toUpperCase();
				
				// store in database
				boolean databaseSuccess = LayerInfoTable.updateFromHash(values);

				// and ingest into solr
				String solrData = solrStream.toString();
				File solrFile = File.createTempFile("solr", ".xml");
				FileWriter writer = new FileWriter(solrFile);
				writer.write(solrData);
				writer.flush();
				writer.close();
				out.write("<p>created solr file " + solrFile.getAbsoluteFile() + "</p>");

				String hostname = request.getServerName() + ":" + request.getServerPort();
				String chmodCommand =  "chmod 666 " + solrFile.getAbsoluteFile(); 
				runCommand(chmodCommand);

				
				String temp = "http://" + hostname + "/solr/update?commit=true -F stream.file=" + solrFile.getAbsoluteFile();
				String solrCommand = "curl " + temp;
				String solrResults = runCommand(solrCommand);
				if (isLayerInSolr(tempName, request))
				{
				    out.write("<p>Solr ingest success</p>");
				    System.out.println("Solr ingest results: " + solrResults);
 				}
				else
				{
				    out.write("<p>Solr ingest FAILED<br/>");
				    out.write("Solr results: " + solrResults + "<p>");
				    System.out.println("Solr FAIL ingest results: " + solrResults);
				}
				
				// finally update geoserver

				String geoserver; // = "http://geoserver01.uit.tufts.edu";
				String workspace; // = "sde";
				String datastore; // = "arcsde10";
				String username; 
				String password; 
				//load a properties file for geoserver info

				Properties properties = new Properties();
				ClassLoader loader = Thread.currentThread().getContextClassLoader();
			        InputStream propertiesStream = loader.getResourceAsStream("config.properties");
			    	properties.load(propertiesStream);

		    	if (((BaseGeoParser.Access)values.get(BaseGeoParser.Key.Access)) == BaseGeoParser.Access.Restricted)
		    	{
					geoserver = properties.getProperty("restrictedGeoServerUrl");
					workspace = properties.getProperty("restrictedGeoServerWorkspace");
					datastore = properties.getProperty("restrictedGeoServerDatastore");
					username = properties.getProperty("restrictedGeoServerUserName");
					password = properties.getProperty("restrictedGeoServerPassword");
		    	}
		    	else
		    	{
					geoserver = properties.getProperty("publicGeoServerUrl");
					workspace = properties.getProperty("publicGeoServerWorkspace");
					datastore = properties.getProperty("publicGeoServerDatastore");
					username = properties.getProperty("publicGeoServerUserName");
					password = properties.getProperty("publicGeoServerPassword");
		    	}


				System.out.println("in ingestHandler with geoserver = " + geoserver + ", username = " + username);
				

				String path = "/rest/workspaces/" + workspace + "/datastores/" + datastore + "/featuretypes.json";
				String postData = "{\"featureType\":{\"name\":\"" + tempName + "\"}}";
				String geoserverCommand = "/usr/bin/curl -XPOST " 
							+ " --user " + username + ":" + password + " " 
							+ " --data " + postData
							+ " --header \"Content-Type:application/json\" "
							+ geoserver + path;

				String returnedGeoserver = runCommand(geoserverCommand);

				System.out.println("returned from geoserver: " + returnedGeoserver.toString());
				out.write("returned from geoserver: " + returnedGeoserver.toString());
		
			}
		}
	}
	else
	{
		out.write("<p>no file found</p>");
	}


%>

<jsp:include page="includes/footer.jspf" />